package com.dofast.bt.mvp.view.activity.shop;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.dofast.bt.R;
import com.dofast.bt.base.BaseMvpActivity;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.mvp.presenter.shop.TeaDetailPresenter;
import com.dofast.bt.mvp.view.activity.basket.BuyListActivity;
import com.dofast.bt.mvp.view.activity.basket.StoreDetailActivity;
import com.dofast.bt.mvp.view.activity.basket.SubmitOderActivity;
import com.dofast.bt.mvp.view.activity.my.MessageListActivity;
import com.dofast.bt.util.Helper;
import com.dofast.bt.util.PresenterFactory;
import com.dofast.bt.util.PresenterLoder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * username:Created by hsj on 2016/12/12 0012.
 * class：
 * 类描述：商品详情界面
 */

public class TeaDetailActivity extends BaseMvpActivity<TeaDetailPresenter, TeaDetailView> implements TeaDetailView {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.iv_good)
    ImageView ivGood;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_introduce)
    TextView tvIntroduce;
    @BindView(R.id.iv_baket)
    ImageView ivBaket;
    @BindView(R.id.iv_collect)
    ImageView ivCollect;
    @BindView(R.id.tv_buy)
    TextView tvBuy;
    @BindView(R.id.iv_detail)
    ImageView ivDetail;

    private TextView tvPopuBasket;

    private TextView tvPopuIntoShop;

    private TextView tvPopuMessage;

    private TextView tvPopuShare;
    private PopupWindow popupWindow;

    @Override
    protected int getResourcesId() {
        return R.layout.activity_tea_detial;
    }

    @Override
    protected void initView() {
        presenter.initData();
        initPopuwindow();
    }

    @Override
    protected void addOnClickListener() {

    }

    @Override
    public Loader<TeaDetailPresenter> onCreateLoader(int id, Bundle args) {
        return new PresenterLoder<>(this, new PresenterFactory<TeaDetailPresenter>() {
            @Override
            public TeaDetailPresenter create() {
                return new TeaDetailPresenter();
            }
        });
    }
    private GoodsBean goodsBean;
    @Override
    public void initInfo(GoodsBean goodsBean,boolean isEnjoy,boolean isAddCar) {
        if (isAddCar){
            ivBaket.setImageResource(R.mipmap.icon_goods_baket_nomal);
        }else {
            ivBaket.setImageResource(R.mipmap.icon_goods_baket_default);
        }
        if(isEnjoy){
            ivCollect.setImageResource(R.mipmap.icon_goods_collect_normal);
        }else {
            ivCollect.setImageResource(R.mipmap.icon_goods_collect_default);
        }
        this.goodsBean = goodsBean;
        //商品名称
        if (!TextUtils.isEmpty(goodsBean.getName())) {
            tvName.setText(goodsBean.getName());
        } else {
            tvName.setText("");
        }

        //介绍
        if (!TextUtils.isEmpty(goodsBean.getContent())) {
            tvIntroduce.setText(goodsBean.getContent());
        } else {
            tvIntroduce.setText("");
        }
        //价格
        tvPrice.setText(goodsBean.getPrice() + "");
        //长图
        Helper.loadIntoUseFitWidth(this,goodsBean.getImg_content(),0,ivDetail);
        //Glide.with(this).load(goodsBean.getImg_content()).into(ivDetail);
        //商品图片
        Glide.with(this).load(goodsBean.getImg_head()).into(ivGood);
    }

    private void initPopuwindow() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentview = inflater.inflate(R.layout.popu_more, null);
        popupWindow = new PopupWindow(contentview, ViewPager.LayoutParams.WRAP_CONTENT, ViewPager.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        tvPopuBasket = ButterKnife.findById(contentview, R.id.tv_popu_basket);
        tvPopuIntoShop = ButterKnife.findById(contentview, R.id.tv_popu_into_shop);
        tvPopuMessage = ButterKnife.findById(contentview, R.id.tv_popu_message);
        tvPopuShare = ButterKnife.findById(contentview, R.id.tv_popu_share);
        clickPopuwindow();
    }

    /**
     * 更多
     */
    private void clickPopuwindow() {
        tvPopuBasket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2016/12/12 0012   加入购物车逻辑
//                Helper.showToast("已加入购物车");
                presenter.buy();
                popupWindow.dismiss();
            }
        });
        tvPopuIntoShop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2016/12/12 0012   进入店铺逻辑
                if (goodsBean != null) {
                    StoreDetailActivity.goTo(TeaDetailActivity.this,goodsBean.getShopId());
                    popupWindow.dismiss();
                }

            }
        });
        tvPopuMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2016/12/12 0012   消息逻辑
                Intent intent = new Intent(TeaDetailActivity.this, MessageListActivity.class);
                startActivity(intent);
                popupWindow.dismiss();
            }
        });
        tvPopuShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2016/12/12 0012   分享逻辑
                Helper.showToast("该功能暂未开放！");
                popupWindow.dismiss();
            }
        });
    }

    @Override
    public void showPopuwindow() {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
        popupWindow.showAsDropDown(ivMore, -168, 0);

    }

    /**
     * @param context
     * @param goodsBean 获取商品详情
     */
    private static String GOODID = "goodId";

    public static void startTeaDetailActivity(Context context, int goodId) {
        Intent intent = new Intent(context, TeaDetailActivity.class);
        intent.putExtra(GOODID, goodId);
        context.startActivity(intent);
    }

    /**
     * @return 获取意图数据
     */
    @Override
    public int getIntentData() {
        return getIntent().getIntExtra(GOODID, -1);
    }

    /**
     * 加入购物车
     */
    @Override
    public void addToBasket() {
        presenter.addToBasket( getIntent().getIntExtra(GOODID, 0));
    }

    /**
     * 取消加入购物车
     */
    @Override
    public void removeFromBasket() {
        presenter.removeFromBasket(getIntentData());
        //ivBaket.setImageResource(R.mipmap.icon_goods_baket_default);
    }

    /**
     * 收藏
     */
    @Override
    public void addToCollect() {
        presenter.addToCollect(getIntentData());
       // ivCollect.setImageResource(R.mipmap.icon_goods_collect_normal);
    }

    /**
     * 取消收藏
     */
    @Override
    public void removeFromCollect() {
        presenter.removeFromCollect(getIntentData());
        //ivCollect.setImageResource(R.mipmap.icon_goods_collect_default);
    }

    /**
     * 购买
     */
    @Override
    public void buy() {
        BuyListActivity.gotoBuyListActivity(TeaDetailActivity.this);
    }

    @Override
    public void initTitle() {

    }

    @Override
    public void gotoActivity(Class<?> tClass, boolean isFinish) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_back, R.id.iv_more, R.id.iv_baket, R.id.iv_collect, R.id.tv_buy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_more:
                presenter.showPopuWindow();
                break;
            case R.id.iv_baket:
                presenter.intoBasket();
                break;
            case R.id.iv_collect:
                presenter.collect();
                break;
            case R.id.tv_buy:
                presenter.buy();
                break;
        }
    }

}
